package com.project.class8_23;

public class DieLock {
    public static void main(String[] args) {
        LockObj o1 = new LockObj("乔丹");
        LockObj o2 = new LockObj("皮蓬");

        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (o1){
                    try {
                        Thread.sleep(200);

                        o2.speak();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (o2){
                    try {
                        Thread.sleep(200);
                        o1.speak();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
}

class LockObj{
    private String name;
    public LockObj(String name){
        this.name = name;
    }

    public synchronized void speak(){
        System.out.println("我叫"+name);
    }
}
